package com.tfswue.chy.service;

import com.tfswue.chy.apis.UserClinet;
import com.tfswue.chy.entity.Order;
import com.tfswue.chy.mapper.OrderMapper;
import com.tfswue.chy.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private UserClinet userClinet;
    public Order getOrderById(Integer id) {
        try {
            System.out.println("开始查询订单，id: " + id);
            
            // 查询订单
            Order order = orderMapper.selectById(id);
            System.out.println("数据库查询结果: " + order);
            
            if (order == null) {
                throw new RuntimeException("订单不存在，id: " + id);
            }
            
            // 调用远程服务获取用户信息
            System.out.println("开始调用 user-server，userId: " + order.getUserId());
            User user = userClinet.getUserById(order.getUserId());
            order.setUser(user);
            
            System.out.println("订单查询成功，包含用户信息");
            return order;
        } catch (Exception e) {
            System.err.println("获取订单失败: " + e.getMessage());
            e.printStackTrace();
            throw new RuntimeException("获取订单失败: " + e.getMessage());
        }
    }
}
